<?php
# +--------------------------------------------------------
# |Description: 阳光公采接口：Access Token处理类
# +--------------------------------------------------------
# |Company: 北京申合信科技发展有限公司
# +--------------------------------------------------------
# |WebSite: www.shenhexin.com
# +--------------------------------------------------------
# |Author: Akon<18363851918@163.com>
# +--------------------------------------------------------
# |Date: 2017年6月19日
# +--------------------------------------------------------

namespace Api\Controller;
use Think\Controller;

class Auth2Controller extends Controller {

    /**
     * 获取Access_token
     * URL http://api.shenhexin.com/api/auth2/access_token
     * METHOD POST
     */
    public function access_token(){
        //$receive = json_decode(file_get_contents('php://input'),true);
        $receive = I('post.');
        $user_info = M('members','sdb_','DB_CONFIG2')->where(array('name'=>$receive['username']))->find();
        if(!$user_info){
            $info = array(
                'success' => "false",
                'desc' => '用户名或密码错误',
            );
            exit(json_encode($info,JSON_UNESCAPED_UNICODE));
        }
        if($receive['username'] != $user_info['name'] || $receive['password'] != $user_info['api_pass']) {
            $info = array(
                "success" => "false2",
                "desc" => "用户名或密码错误"
            );
            $this->ajaxReturn($info);
        }
        if(($my_sign = md5_sign($receive['username'],$receive['password'],$receive['timestamp'])) != $receive['sign']){
            $info = array(
                "success" => "false",
                "desc" => "sign error",
                "timestamp" => I('username')
            );
            $this->ajaxReturn($info);
        }
        //生成Access_token规则：对用户名和密码进行md5加密，连接当前时间戳
        $access_token = generate_token($receive['username'],$receive['password']);
        $info = array(
            "success" => "true",
            "access_token" => $access_token,
            "expires_at" => strtotime($receive['timestamp']) + 43200
        );
        $res = M('members','sdb_','DB_CONFIG2')->where(array('name'=>$receive['username']))->setField(array('access_token'=>$access_token,'create_time'=>time(),'expire_time'=>$info['expires_at']));
        $this->ajaxReturn($info);
    }

}